home *** CD-ROM | disk | FTP | other *** search
- /* RDLOD.C -- Reads Motorola DSP56000 linker load files
- *
- * Copyright (C) by Alef Null 1990, 1991
- * Author(s): Jarkko Vuori, OH2LNS
- * Modification(s):
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "dspio.h"
-
- static FILE *fd;
-
-
- /* add extension to the filename */
- static char *AddExtension(char *FileName, const char *Extension) {
- static char Name[_MAX_PATH];
- char *s1;
-
- /* copy basename */
- s1 = Name;
- while(*FileName && *FileName != '.')
- *s1++ = *FileName++;
-
- /* copy extension (if there are already no extension) */
- strcpy(s1, !*FileName ? Extension : FileName);
-
- return(Name);
- }
-
-
- /*
- * Opens linker file
- */
- int OpenLodFile(char *name) {
- if (!(fd = fopen(AddExtension(name, ".lod"), "rb"))) {
- fprintf(stderr, "rdlod error: can't open file '%s'\n", AddExtension(name, ".lod"));
- return (-1);
- } else {
- setvbuf(fd, NULL, _IOFBF, 8192);
- return (0);
- }
- }
-
-
- /* read next token from input */
- static char *ReadToken(void) {
- static char line[80];
- char c, *p;
-
- #define WHITESPACE(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n')
-
- /* flush leading whitespace out */
- do {
- fread(&c, 1, 1, fd);
- } while (WHITESPACE(c) && !feof(fd));
-
- /* then read next token */
- p = line;
- while (p < &line[80] && !feof(fd) && !WHITESPACE(c)) {
- *p++ = c;
- fread(&c, 1, 1, fd);
- }
- *p = '\0';
-
- return (line);
- }
-
- /* convert ASCII-Hex argument to long number */
- static long HexToLong(char *data) {
- long val;
-
- sscanf(data, "%lX", &val);
-
- return (val);
- }
-
- /*
- * Read next word
- */
- int ReadWord(DATASPACE *space, unsigned *address, long *word) {
- static DATASPACE currentspace;
- static unsigned currentaddress;
- static enum {
- hunt, spaceheader, collectdata, end
- } state;
- char *token;
-
- while (1)
- switch (state) {
- case hunt:
- /* search preamble */
- if (strcmp(ReadToken(), "_START")) {
- fprintf(stderr, "rdlod error: expecting _START, not a valid Motorola load file\n");
- return (-1);
- } else {
- ReadToken(); ReadToken(); ReadToken(); // flush out _START arguments
-
- if (strcmp(ReadToken(), "_DATA")) {
- fprintf(stderr, "rdlod error: expecting _DATA, not a valid Motorola load file\n");
- return (-1);
- }
- state = spaceheader;
- }
- break;
-
- case spaceheader:
- /* data space header */
- switch (*ReadToken()) {
- case 'P': currentspace = p; break;
- case 'X': currentspace = x; break;
- case 'Y': currentspace = y; break;
- default:
- fprintf(stderr, "rdlod error: illegal data space, not a valid Motorola load file\n");
- return (-1);
- break;
- }
-
- currentaddress = (unsigned)HexToLong(ReadToken());
-
- state = collectdata;
- break;
-
- case collectdata:
- /* read next data */
- token = ReadToken();
- if (!strcmp(token, "_DATA"))
- state = spaceheader;
- else if (!strcmp(token, "_END"))
- state = end;
- else if (!*token) {
- fprintf(stderr, "rdlod error: inputfile too short, not a valid Motorola load file\n");
- return (-1);
- } else {
- *space = currentspace;
- *address = currentaddress++;
- *word = HexToLong(token);
- return (0);
- }
- break;
-
- case end:
- /* end of inputfile */
- return (1);
- break;
- }
- }
-
-
- /*
- * Closes linker file
- */
- int CloseLodFile(void) {
- fclose(fd);
-
- return (0);
- }
-